package com.coolgor.xcode;

import java.io.*;
import java.util.List;

public class StackDto {

    public static void run(String basePath, List<String[]> context) throws IOException {
        String outputPath = basePath + XcoderConfig.OutputPath + "/dto";
        String tableName = null;
        String className = null;
        String keyColumn = null;
        String keyParam = null;
        for (int i = 0; i < context.size(); i++) {
            switch (context.get(i)[0].toLowerCase()){
                case "table":
                    tableName = context.get(i)[1];
                    className = StringUtil.firstUpper(context.get(i)[2]);
                    break;
                case "key":
                    keyColumn = context.get(i)[1];
                    keyParam = context.get(i)[2];
                    break;
            }
        }

        // File writing ###############
        File file =new File(outputPath);
        if (!file.exists()) file.mkdirs();
        file = new File(outputPath+"/"+className+"Execution.java");
        if (!file.exists()) file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        bw.newLine();
        bw.write("import java.util.List;\n");
        bw.newLine();
        bw.newLine();
        bw.write("public class "+className+"Execution {\n");
        bw.newLine();
        bw.write("\t// 结果状态 \n");
        bw.write("\tprivate int state;\n");
        bw.write("\t// 结果状态标识 \n");
        bw.write("\tprivate String stateInfo;\n");
        bw.write("\t// 执行结果的数量 \n");
        bw.write("\tprivate int count;\n");
        bw.write("\t// 操作的对象(增删改的时候用到)\n");
        bw.write("\tprivate "+className+" "+StringUtil.firstLower(className)+";\n");
        bw.write("\t// 获取的结果列表(查询列表的时候使用)\n");
        bw.write("\tprivate List<"+className+"> "+StringUtil.firstLower(className)+"List;\n");
        bw.newLine();
        bw.write("\t// 构造器\n");
        bw.write("\tpublic "+className+"Execution() {}\n");
        bw.newLine();
        bw.write("\t// 操作失败的时候使用的构造器\n");
        bw.write("\tpublic "+className+"Execution("+className+"OperationStateEnum stateEnum) {\n");
        bw.write("\t\tthis.state = stateEnum.getState();\n");
        bw.write("\t\tthis.stateInfo = stateEnum.getStateInfo();\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\t// 增删改 操作成功的时候使用的构造器\n");
        bw.write("\tpublic "+className+"Execution("+className+"OperationStateEnum stateEnum, "+className+" "+StringUtil.firstLower(className)+") {\n");
        bw.write("\t\tthis.state = stateEnum.getState();\n");
        bw.write("\t\tthis.stateInfo = stateEnum.getStateInfo();\n");
        bw.write("\t\tthis."+StringUtil.firstLower(className)+" = "+StringUtil.firstLower(className)+";\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\t// 查询 操作成功的时候使用的构造器\n");
        bw.write("\tpublic "+className+"Execution("+className+"OperationStateEnum stateEnum, List<"+className+"> "+StringUtil.firstLower(className)+"List) {\n");
        bw.write("\t\tthis.state = stateEnum.getState();\n");
        bw.write("\t\tthis.stateInfo = stateEnum.getStateInfo();\n");
        bw.write("\t\tthis."+StringUtil.firstLower(className)+"List = "+StringUtil.firstLower(className)+"List;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.newLine();
        bw.write("\t// 下面都是getter 和setter\n");
        bw.newLine();
        bw.write("\tpublic int getState() {\n");
        bw.write("\t\treturn state;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic void setState(int state) {\n");
        bw.write("\t\tthis.state = state;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic String getStateInfo() {\n");
        bw.write("\t\treturn stateInfo;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic void setStateInfo(String stateInfo) {\n");
        bw.write("\t\tthis.stateInfo = stateInfo;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic int getCount() {\n");
        bw.write("\t\treturn count;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic void setCount(int count) {\n");
        bw.write("\t\tthis.count = count;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic "+className+" get"+className+"() {\n");
        bw.write("\t\treturn "+StringUtil.firstLower(className)+";\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic void set"+className+"("+className+" "+StringUtil.firstLower(className)+") {\n");
        bw.write("\t\tthis."+StringUtil.firstLower(className)+" = "+StringUtil.firstLower(className)+";\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic List<"+className+"> get"+className+"List() {\n");
        bw.write("\t\treturn "+StringUtil.firstLower(className)+"List;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic void set"+className+"List(List<"+className+"> "+StringUtil.firstLower(className)+"List) {\n");
        bw.write("\t\tthis."+StringUtil.firstLower(className)+"List = "+StringUtil.firstLower(className)+"List;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("}");
        bw.close();
    }
}
